using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace lwd800.Views
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();
            // 默认焦点到密码框
            txtPassword.Focus();
        }

        private void txtPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            pwdWatermark.Visibility = string.IsNullOrEmpty(txtPassword.Password) ? Visibility.Visible : Visibility.Collapsed;
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            // 获取用户类型
            string userType = rbAdmin.IsChecked == true ? "管理员" : "操作员";
            string password = txtPassword.Password;

            // 校验
            if (string.IsNullOrEmpty(password))
            {
                ShowError("密码不能为空");
                return;
            }

            if (userType == "管理员" && password == "123")
            {
                var mainWin = new MainWindow();
                mainWin.Show();
                this.Close();
            }
            else if (userType == "操作员" && password == "111")
            {
                var mainWin = new MainWindow();
                mainWin.Show();
                this.Close();
            }
            else
            {
                ShowError("用户名或密码错误");
            }
        }

        private void ShowError(string message)
        {
            lblError.Text = message;
            lblError.Visibility = Visibility.Visible;
        }

        // 回车键登录
        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnLogin_Click(sender, e);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // 可选：窗口加载时初始化
        }
    }
}